//
//  ZanView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/17.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ZanView.h"
#import "LikesModel.h"
#import "EvaluateModel.h"
#import "UIButton+WebCache.h"
#define BtnWith ((SCREEN_WIDTH-7*11)/10)
#define ZanShangWith 70
#define shangColor RGBA(244, 204, 22, 1)
#define ZanOrAwardWith ((SCREEN_WIDTH-7*11)/10+8)
@interface ZanView ()
@property (nonatomic,strong) AppDelegate *app;
@property (nonatomic,strong) NSMutableArray *hahhaha;
@property (nonatomic,strong) UIView *lin;

/**
 *  显示赞的人的头像 的父视图
 */
@property (nonatomic,strong) UIView *bacView;
/**
 *  点赞Btn
 */
@property (nonatomic,strong) UIButton *zanBtn;
/**
 *  赏btn
 */
@property (nonatomic,strong) UIButton *awardBtn;
/**
 *  圆圈显示赏的lab
 */
@property (nonatomic,strong) UILabel *shangViwelel;
/**
 *  赏 数据label  显示威望 金钱等值
 */
@property (nonatomic,strong) UILabel *shangCountLabe;
/**
 *  威望、金钱、贡献、钓币
 */
@property (nonatomic,copy) NSString *shangText;
@property (nonatomic,assign) BOOL IsShang;
@end

@implementation ZanView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _headAry = [NSMutableArray array];
        self.shangText = @"";
    }
    return self;
}

-(UIView *)lin{
    if (!_lin) {
        _lin = [[UIView alloc]initWithFrame:CGRectMake(0, Height(_zanBtn)+13, SCREEN_WIDTH, 0.4)];
        _lin.backgroundColor = linColor;
        [self addSubview:_lin];
    }
    return _lin;
}

-(void)setModel:(SelectthreadModel *)model{
    _model = model;

    self.app = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if (_model!=nil) {

           BOOL likecount = _model.likes.count!=0;
          BOOL scorecount = _model.evaluate_score.count!=0;
           self.height =  ZanOrAwardWith*2+ZanOrAwardWith*likecount+ZanOrAwardWith*scorecount;

           self.footView.backgroundColor = RGBA(235, 235, 241, 1);
        //根据数据计算 赏的坐标
        self.footView.y = H(self)-10;
        self.awardBtn.backgroundColor = [UIColor whiteColor];
        self.zanBtn.selected = _model.yes_support.integerValue;
        
        self.lin.alpha = likecount||scorecount;
        
        [self.zanBtn setTitle:_model.recommend_add forState:UIControlStateNormal];
        if (_model.likes.count!=0) {
            self.headAry = [NSMutableArray arrayWithArray:_model.likes];
        }

            if (_model.evaluate_score.count!=0) {
                
                self.shangViwelel.y =   Y(_footView)-BtnWith-8;
                self.shangCountLabe.y =Y(_shangViwelel);

                self.shangViwelel.textColor = shangColor;

                for (EvaluateModel *model in _model.evaluate_score) {
                    NSString *sstr = [NSString stringWithFormat:@" %@+%@ ",model.extcredits,model.totoal_rate];
                    self.shangText = [self.shangText stringByAppendingString:sstr];
                }
                self.shangCountLabe.text = self.shangText;
                self.shangText = @"";
            }
     }
}

-(UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, H(self)-10, SCREEN_WIDTH, 10)];
         [self addSubview:_footView];
    }
    return _footView;
}

//在详情页打赏完成 刷新楼主的分值
-(void)setAwardAry:(NSArray *)awardAry{
    _awardAry = awardAry;
    
     self.shangViwelel.textColor = shangColor;

        for (int i=0; i<_model.evaluate_score.count; i++) {
            
            EvaluateModel *model = _model.evaluate_score[i];
            for (EvaluateModel *evalue in awardAry) {
                if ([evalue.extcredits isEqualToString:model.extcredits]) {
                    
                    NSInteger count = model.totoal_rate.integerValue+evalue.totoal_rate.integerValue;
                    evalue.totoal_rate = [NSString stringWithFormat:@"%ld",(long)count];
                }
            }
        }
        
        for (EvaluateModel *model in awardAry) {
            NSString *sstr = [NSString stringWithFormat:@" %@+%@ ",model.extcredits,model.totoal_rate];
            self.shangText = [self.shangText stringByAppendingString:sstr];
        }
        
        self.shangCountLabe.text = self.shangText;
        _model.evaluate_score = [awardAry copy];
        self.IsShang = YES;
        self.shangText = @"";
}

/**
 *  点赞 btn 的父视图
 */
-(UIButton *)zanBtn{

    if (!_zanBtn) {
        _zanBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-ZanShangWith-10, 2, ZanShangWith, 30)];
        _zanBtn.layer.borderWidth = 0.7;
        _zanBtn.layer.borderColor = mainColor.CGColor;
        radius(_zanBtn, 2);
    
        _zanBtn.titleLabel.font = font(SCREEN_WIDTH * 0.055);
        _zanBtn.titleEdgeInsets = UIEdgeInsetsMake(5, 7, 0, 0);
        
        [_zanBtn setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateNormal];
        [_zanBtn setTitleColor:mainColor forState:UIControlStateNormal];
        [_zanBtn setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected | UIControlStateHighlighted];
        [_zanBtn setTitleColor:mainColor forState:UIControlStateSelected];
        [_zanBtn setTitleColor:mainColor forState:UIControlStateSelected | UIControlStateHighlighted];
        
        [_zanBtn addTarget:self action:@selector(Zanclicklick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_zanBtn];
    }
    return _zanBtn;
}

-(void)Zanclicklick:(UIButton *)sender{
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        if (!sender.selected) {
            sender.selected = YES;
            
            [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:AticlePramis,(long)_model.tid.integerValue,self.formHash] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
                if (error) {
                    if ([self.deleget respondsToSelector:@selector(praimsFieleWhitMessage:)]) {
                        [self.deleget praimsFieleWhitMessage:@"点赞失败"];
                    }
                }else{
                    if ([responseObject[@"status"]isEqualToString:@"SUCCESS"]) {
                        NSInteger count = sender.titleLabel.text.integerValue+1;
                        
                        [sender setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
                        
                        [UIView animateWithDuration:0.5 animations:^{
                            
                            sender.imageView.transform = CGAffineTransformScale(sender.imageView.transform, 0.65, 0.65);
                        } completion:^(BOOL finished) {
                            
                            [self IsZanYES];
                            [self.deleget ZanClickISLogin:YES];
                        }];
                    }else{
                        if ([self.deleget respondsToSelector:@selector(praimsFieleWhitMessage:)]) {
                            [self.deleget praimsFieleWhitMessage:responseObject[@"message"]];
                        }
                    }
                }
            }];
        }else{
        
         [self.deleget praimsFieleWhitMessage:@"您已经点过赞，不能重复点赞"];
        }
        
    }else{
        [self.deleget ZanClickISLogin:NO];
    }
}

/**
 *  如果点了赞
 */
-(void)IsZanYES{
    
    self.IsZan = YES;
    self.zanBtn.selected = YES;
    
    LikesModel *model = [[LikesModel alloc]init];
    model.avatar = self.app.userModel.avatar;
    model.user_id = self.app.userModel.uid;
    
    self.hahhaha  = [NSMutableArray arrayWithObject:model];
    [self.hahhaha addObjectsFromArray:_model.likes];
    _model.likes = [self.hahhaha mutableCopy];
   
    NSInteger count =  _model.recommend_add.integerValue+1;
    _model.recommend_add = [NSString stringWithFormat:@"%ld",(long)count];
    
    if ([self.deleget respondsToSelector:@selector(Succerefresh)]) {
        [self.deleget Succerefresh];
    }
}

/**
 *  点赏btn
 */
-(UIButton *)awardBtn{
    
    if (!_awardBtn) {
        _awardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _awardBtn.frame = CGRectMake(SCREEN_WIDTH/2+10, 2, ZanShangWith, 30);
        _awardBtn.layer.borderColor = shangColor.CGColor;
        _awardBtn.layer.borderWidth = 0.7;
        radius(_awardBtn, 2);
        
        [_awardBtn setImage:[UIImage imageNamed:@"iconfont-dashang"] forState:UIControlStateNormal];
        [_awardBtn setTitle:@"赏" forState:UIControlStateNormal];
        [_awardBtn setTitleColor:shangColor forState:UIControlStateNormal];
        
        _awardBtn.titleLabel.font = font(SCREEN_WIDTH*0.05);
        _awardBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        
        [_awardBtn addTarget:self action:@selector(awardBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_awardBtn];
    }
    return _awardBtn;
}
/**
 *  点赏代理方法
 */
-(void)awardBtnClick{
    
    BOOL isLogin;
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        isLogin = YES;
    }else{
        isLogin = NO;
    }
    [self.deleget awardClick:isLogin];
}

/**
 *  显示点赞人的头像
 */
-(void)setHeadAry:(NSMutableArray *)headAry{
    _headAry = [headAry copy];
    
    [_bacView removeFromSuperview];
    _bacView = [[UIView alloc]initWithFrame:CGRectMake(BtnWith+7+7, Height(_lin)+8, SCREEN_WIDTH-(BtnWith+7+7)*2, BtnWith)];
    [self addSubview:_bacView];
    
    NSInteger count = _headAry.count;
    if (_headAry.count>=8) {
        count = 8;
    }
    
    for (int i = 0; i<count; i++) {
        LikesModel *model = _headAry[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((7+BtnWith)*i, 0, BtnWith, BtnWith);
        btn.backgroundColor = [UIColor lightGrayColor];
        radius(btn, BtnWith/2);
        [btn sd_setImageWithURL:[NSURL URLWithString:model.avatar] forState:UIControlStateNormal];
        [self.bacView addSubview:btn];
        
        btn.tag = i+10000;
        [btn addTarget:self action:@selector(ShowSpace:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.Zan.text  = @"赞";
    _Zan.layer.borderColor = mainColor.CGColor;
    self.count.text =  _model.recommend_add;
}

-(void)ShowSpace:(UIButton *)btn{

    LikesModel *model = _headAry[btn.tag-10000];
    [self.deleget ShowSpaceClick:model.user_id];
}

/**
 *  显示 赞 字
 */
-(UILabel *)Zan{
    if (!_Zan) {
        _Zan = [[UILabel alloc]initWithFrame:CGRectMake(7, Height(_lin)+8, BtnWith, BtnWith)];
        _Zan.layer.borderColor = [UIColor clearColor].CGColor;
        _Zan.layer.borderWidth = 1;
        _Zan.font = font(SCREEN_WIDTH*0.04);
        _Zan.textAlignment = NSTextAlignmentCenter;
        radius(_Zan, BtnWith/2);
        _Zan.textColor = mainColor;
        [self addSubview:_Zan];
    }
        return _Zan;
}

/**
 *  头像后面 显示被赞的数量
 */
-(UILabel *)count{
    if (!_count) {
        _count = [[UILabel alloc]initWithFrame:CGRectMake(W(self)-BtnWith-7, Height(_lin)+8, BtnWith, BtnWith)];
        _count.textColor = [UIColor blackColor];
        _count.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_count];
        _count.font = font(SCREEN_WIDTH*0.045);
    }
        return _count;
}

/**
 *  显示赏字 label
 */
-(UILabel *)shangViwelel{
    if (!_shangViwelel) {
        _shangViwelel = [[UILabel alloc]initWithFrame:CGRectMake(7, Y(_footView)-BtnWith-8, BtnWith, BtnWith)];
        _shangViwelel.layer.borderColor = shangColor.CGColor;
        _shangViwelel.layer.borderWidth = 1;
        _shangViwelel.font = font(SCREEN_WIDTH*0.04);
        _shangViwelel.textAlignment = NSTextAlignmentCenter;
        radius(_shangViwelel, BtnWith/2);
        _shangViwelel.text = @"赏";
        [self addSubview:_shangViwelel];
    }
    return _shangViwelel;
}

-(UILabel *)shangCountLabe{

    if (!_shangCountLabe) {
        _shangCountLabe = [[UILabel alloc]initWithFrame:CGRectMake(BtnWith+7+7, Y(_shangViwelel), SCREEN_WIDTH-(BtnWith+7+7), BtnWith)];
        _shangCountLabe.font = font(SCREEN_WIDTH*0.045);
        [self addSubview:_shangCountLabe];
    }
    return _shangCountLabe;
}
@end